package cn.itcast.dao.impl;

import cn.itcast.dao.IUserDao;
import cn.itcast.pojo.User;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserDaoImpl implements IUserDao {
    private SqlSession sqlSession;

    public UserDaoImpl(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }

    @Override
    public User queryUserById(Long id) {
        User user = sqlSession.selectOne("UserDaoMapper.queryUserById", id);
        return user;
    }

    @Override
    public List<User> queryUserList() {
        List<User> list = sqlSession.selectList("UserDaoMapper.queryUserList");
        return list;
    }


    @Override
    public void insertUser(User user) {
        //返回值是该方法操作的记录数
        int count = sqlSession.insert("UserDaoMapper.insertUser", user);
        System.out.println(count);
        // sqlSession.commit();//提交事务
    }

    @Override
    public void updateUser(User user) {
        //返回值是该方法操作的记录数
        int count = sqlSession.update("UserDaoMapper.updateUser", user);
        System.out.println(count);
        // sqlSession.commit();//提交事务
    }

    @Override
    public void deleteUserById(Long id) {
        //返回值是该方法操作的记录数
        int count = sqlSession.delete("UserDaoMapper.deleteUserById", id);
        System.out.println(count);
        // sqlSession.commit();//提交事务
    }
}
